<?php
session_start();
require_once( 'class_db.php' );
 
$type = new M_ClassRoom();
$user_id = $_SESSION['userid'];

if (isset($_POST['m'])  &&  $_POST['m']!=""){
	if ($_POST['m']=='load_level'){
	  
      $result=$type->loadLevel();
      $rows=$type->getRowCount();
      $i=1;
      $html="";
      if ($rows>0){
           $data = array();
            while($row = $result->FetchRow()){
               $data[] = array(0,$row['id'],$row['name'],$row['alian'], $row['status']) ;
            }
        }else{
			$data = array();
        }
    
        $arr = array('aaData'=>$data);
        
        echo json_encode($arr);
	}else if ($_POST['m']=='add_level'){
		$ar = array();
		$ar['name']=  $_POST['t'];
		$ar['status'] = $_POST['s'];
		$ar['alian']=$_POST['a'];
		$ar['createdate'] =date( "Y/m/d H:i:s" );
		$ar['createby'] = $user_id;

		$id=$type->InsertDatalevel($ar);
	}else if ($_POST['m']=='edit_level'){
		$ar = array();
		$ar['name']=  $_POST['t'];
		$ar['alian']=$_POST['a'];
		$ar['status'] = $_POST['s'];
		$ar['updatedate'] =date( "Y/m/d H:i:s" );
		$ar['updateby'] = $user_id;
  
		$strcipt = $type->updateDatalevel($_POST['id'],$ar);
		// echo json_encode($strcipt);
	}else if ($_POST['m']=='del_level'){
		 $id=$_POST['id'];
		 $type->deleDatalevel($id);
		 
	}else if ($_POST['m']== 'load'){
		$id = $_POST['id'];
		$year =$_POST['year'];
      //$type->setTable($_POST['tb']);  
      $result=$type->loadData($id, $year);
      $rows=$type->getRowCount();
      $i=1;
      $html="";
      if ($rows>0){
           $data = array();
            while($row = $result->FetchRow()){
               $data[] = array(0,$row['id'],$row['class_id'],$row['year'],$row['name'], $row['room'], $row['amount'],$row['title1'].' '.$row['teacher_1'],$row['title2'].' '.$row['teacher_2'],$row['status'],$row['t1'],$row['t2']) ;
            }
        }else{
			$data = array();
        }
    
        $arr = array('aaData'=>$data);
        
        echo json_encode($arr);
      
    }else if ($_POST['m']=='add'){
        $ar = array();
		$ar['year'] = $_POST['year'];
		$ar['class_id']=  $_POST['level'];
		$ar['room'] = $_POST['room'];
		$ar['status'] = $_POST['s'];
		$ar['amount'] = $_POST['amt'];
		$ar['teacher_1'] = $_POST['teacher_1'];
		$ar['teacher_2'] = $_POST['teacher_2'];
		$ar['createdate'] =date( "Y/m/d H:i:s" );
		$ar['createby'] = $user_id;
	 
		$id=$type->InsertData($ar);
		
    }else if ($_POST['m']=='edit'){
		$ar = array();
		$ar['year'] = $_POST['year'];
		$ar['class_id']=  $_POST['level'];
		$ar['room'] = $_POST['room'];
		$ar['status'] = $_POST['s'];
		$ar['amount'] = $_POST['amt'];
		$ar['teacher_1'] = $_POST['teacher_1'];
		$ar['teacher_2'] = $_POST['teacher_2'];
		$ar['updatedate'] =date( "Y/m/d H:i:s" );
		$ar['updateby'] = $user_id;
		$strcipt = $type->updateData($_POST['id'],$ar);
		// echo json_encode($strcipt);
	}
	else if ($_POST['m']=='del'){
         $id=$_POST['id'];
		// $type->setTable($_POST['tb']);  
		 $type->deleData($id);
		 
    }
}else{
	if (isset($_GET['m']) && $_GET['m'] !="" ){
			if ($_GET['m']=='c'){ 
				echo json_encode( $type->getClassLevel());
			}else if ($_GET['m']=='emp'){
				echo json_encode( $type->getEmployee());
			}else if ($_GET['m'] =='year'){
			  echo json_encode($type->getYear_Term());
			}else if ($_GET['m'] =='curryear'){
				echo $type->getCurrentYear();
			}
	}
   
}
 
?>